package com.vdong.trade.trading.service.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vdong.trade.trading.common.constants.TradingConstants;
import com.vdong.trade.trading.common.enums.AmountTypeEnum;
import com.vdong.trade.trading.entity.dto.result.AmountResultDTO;
import com.vdong.trade.trading.entity.mo.AmountUpdateLogWithBLOBsMO;
import com.vdong.trade.trading.respository.AmountUpdateLogMapper;
import com.vdong.trade.trading.service.AmountService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.lamb.framework.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Optional;


@Aspect
@Component
@Slf4j
public class AmountLogAspect {

    @Autowired
    private AmountUpdateLogMapper amountUpdateLogMapper;

    @Autowired
    private AmountService amountService;

    @Pointcut("@annotation(com.vdong.trade.trading.common.annotation.AmountLogAnnotation)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint point) {
        Object result = null;
        try {
            result = point.proceed();
        } catch (Throwable throwable) {
            log.error("AmountLogAspect.saveLog error:", throwable);
        }
        this.saveLog(point, result);
        return result;
    }

    /**
     * 保存日志
     *
     * @param point
     */
    private void saveLog(ProceedingJoinPoint point, Object result) {
        try {
            Object[] args = point.getArgs();
            JSONObject params = null;
            for (Object arg : args) {
                params = JSON.parseObject(JSON.toJSONString(arg));
            }

            if (params == null) {
                return;
            }

            AmountResultDTO amountResultDTO = amountService.selectAmountMOByUserId(
                    Long.valueOf(params.get("userId").toString()), params.get("userType").toString());

            AmountUpdateLogWithBLOBsMO mo = new AmountUpdateLogWithBLOBsMO();
            mo.setUserId(Long.valueOf(params.get("userId").toString()));
            mo.setUserType(Short.valueOf(params.get("userType").toString()));
            Short amountType = Short.valueOf(params.get("amountType").toString());
            mo.setAmountSnapshoot(JSON.toJSONString(amountResultDTO));
            mo.setAmountType(amountType);
            mo.setRequest(JSON.toJSONString(params));
            mo.setCreateTime(new Date());
            Optional optional = (Optional) result;
            if (optional.isPresent()) {
                mo.setResponse(JSON.toJSONString(result));
            } else {
                mo.setResponse(TradingConstants.SUCCESS);
            }
            mo.setRemark(AmountTypeEnum.getEnumByType(amountType).getDesc());
            amountUpdateLogMapper.insertSelective(mo);
        } catch (Exception e) {
            log.error("AmountLogAspect.saveLog error:", e);
        }
    }
}
